using System;
using System.Collections.Generic;
using System.Text;
using Barrage.GUI;
using Barrage.GUI.Elements;
using Spacedash.Logic;

namespace Spacedash.Game.Subgames
{
    static class EditProfileElements
    {
        public const string SaveButton = "SAVE_BUTTON";
        public const string ProfileName = "PROFILE_NAME";
        public const string SpeedSlider = "SPEED";
        public const string PursuitSlider = "PURSUIT";
        public const string HandlingSlider = "HANDLING";
        public const string ModelSelector = "MODEL_SELECTOR";
    }
    class EditProfileSection:GUISection
    {
        GUISlider speedSlider;
        GUISlider pursuitSlider;
        GUISlider handlingSlider;
        GUITextInput nameInput;
        GUISelector modelSelector;

        public override int Id
        {
            get { return (int)GameState.EditProfile; }
        }
        
        Profile currentProfile;
        public EditProfileSection(Profile profile, SpacedashGame parent)
            : base("Profile", parent)
        {
            RegisterButtonEvent(EditProfileElements.SaveButton, new GUIButtonHandler(SaveButtonClick));
            currentProfile = profile;

            nameInput = (GUITextInput)Layout.Elements[EditProfileElements.ProfileName];
            speedSlider = (GUISlider)Layout.Elements[EditProfileElements.SpeedSlider];
            pursuitSlider = (GUISlider)Layout.Elements[EditProfileElements.PursuitSlider];
            handlingSlider = (GUISlider)Layout.Elements[EditProfileElements.HandlingSlider];
            modelSelector = (GUISelector)Layout.Elements[EditProfileElements.ModelSelector];
            
            speedSlider.OnInteraction += new GUISliderHandler(OnSpeedInteraction);
            pursuitSlider.OnInteraction += new GUISliderHandler(OnPursuitInteraction);
            handlingSlider.OnInteraction += new GUISliderHandler(OnHandlingInteraction);

            int i = 0;
            foreach (string name in ShipModel.GetAvailableModels())
            {
                modelSelector.addOption(i++, name);
                if (name == currentProfile.ShipModel)
                    modelSelector.CurrentOption = i-1;
            }

            nameInput.Text = currentProfile.Name;
            speedSlider.Percent = currentProfile.Speed;
            pursuitSlider.Percent = currentProfile.Pursuit;
            handlingSlider.Percent = currentProfile.Handling;
        }

        void OnSpeedInteraction(string buttonId, float value)
        {
            currentProfile.Speed = value;
            pursuitSlider.Percent = currentProfile.Pursuit;
            handlingSlider.Percent = currentProfile.Handling;
        }
        void OnHandlingInteraction(string buttonId, float value)
        {
            currentProfile.Handling = value;
            speedSlider.Percent = currentProfile.Speed;
            pursuitSlider.Percent = currentProfile.Pursuit;
        }
        void OnPursuitInteraction(string buttonId, float value)
        {
            currentProfile.Pursuit = value;
            speedSlider.Percent = currentProfile.Speed;
            handlingSlider.Percent = currentProfile.Handling;
        }

        void SaveButtonClick(string buttonId)
        {
            currentProfile.Speed = speedSlider.Percent;
            currentProfile.Pursuit = pursuitSlider.Percent;
            currentProfile.Handling = handlingSlider.Percent; 
            currentProfile.Name = nameInput.Text;
            currentProfile.ShipModel = modelSelector.CurrentOptionValue;

            parent.NextGameSectionId = (int)GameState.Profiles;
            
            currentProfile.Save();
        }

        public override void MainLoop()
        {
            base.MainLoop();
        }
    }
}
